package com.yylany.controller;

import com.yylany.emunt.CommonConstants;
import com.yylany.pojo.Agency;
import com.yylany.pojo.User;
import com.yylany.pojo.vo.UserVO;
import com.yylany.result.ResultMsg;
import com.yylany.service.AgencyService;
import com.yylany.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * User  yyl
 * Description: house
 * Created by yaoyonglong on 2018/11/3 23:36
 */
@Controller
@RequestMapping("accounts")
public class UserController {


    @Autowired
    private UserService userService;
    @Autowired
    private AgencyService agencyService;


    /**
     * 注册验证，发送邮件，验证失败后重定向到注册页面
     *
     * @param user
     * @param modelMap
     * @return
     */
    @RequestMapping("/register")
    public String accountsRegister(UserVO user, ModelMap modelMap) {
        List<Agency> agencies = agencyService.getAllAgency();
        modelMap.put("agencyList", agencies);

        if (user == null || user.getName() == null) {
            return "/user/accounts/register";
        }
        // 用户验证

        ResultMsg validate = UserHelper.validate(user);
        // 判断是否成功
        if (validate.isSuccess() && userService.addAccount(user)) {
            modelMap.put("email", user.getEmail());
            return "/user/accounts/registerSubmit";
        } else {
            return "redirect:/accounts/register?" + validate.asUrlParams();

        }
    }

    @RequestMapping("/verify")
    public String verify(String key) {
        boolean result = userService.enable(key);
        if (result) {
            return "redirect:/accounts/register?" + ResultMsg.successMsg("激活成功").asUrlParams();
        } else {
            return "redirect:/index?" + ResultMsg.errorMsg("激活失败，请重试").asUrlParams();
        }
    }


    @RequestMapping("/signin")
    public String signin(HttpServletRequest request) {

        String email = request.getParameter("username");
        String password = request.getParameter("password");
        String target = request.getParameter("target");

        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            return "/user/accounts/signin";
        }
        User user = userService.auth(email, password);

        if (user == null) {
            return "redirect:/accounts/signin?" + "target=" + target + "&username=" + email + "&"
                    + ResultMsg.errorMsg("用户名或密码错误，请重新输入").asUrlParams();

        } else {

            HttpSession session = request.getSession();
            session.setAttribute(CommonConstants.USER_ATTRIBUTE, user);
            session.setAttribute(CommonConstants.PLAIN_USER_ATTRIBUTE, user);
            return StringUtils.isNoneBlank(target) ? "redirect:" + target : "redirect:/index";
        }

    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
        return "redirect:/index";
    }


    // 个人中心页,提供页面，以及更新信息
    @RequestMapping("/profile")
    public String profile(HttpServletRequest request, User updateUser, ModelMap model) {

        if (StringUtils.isEmpty(updateUser.getEmail())) {

            return "/user/accounts/profile";
        }
        userService.updateUser(updateUser, updateUser.getEmail());
        User user = userService.getUserByEmail(updateUser.getEmail());
        request.getSession(true).setAttribute(CommonConstants.USER_ATTRIBUTE, user);
        return "redirect:/accounts/profile?" + ResultMsg.successMsg("更新成功").asUrlParams();

    }


    // 更新密码操作
    @RequestMapping("/changePassword")
    public String changePassword(String email, String password, String newPassword, String confirmPassword, ModelMap map) {

        User auth = userService.auth(email, password);
        if (auth == null || !newPassword.equals(confirmPassword)) {
            return "redirect:/accounts/profile?" + ResultMsg.errorMsg("密码错误或密码不相等").asUrlParams();
        }

        return "redirect:/accounts/profile?" + ResultMsg.successMsg("更新成功").asUrlParams();

    }

}
